#ifndef __FAN_CTRL_H
#define __FAN_CTRL_H

#include <stdint.h>

//#define LED_OFF         (gpio_bit_set(GPIOB,GPIO_PIN_15))
//#define LED_ON          (gpio_bit_reset(GPIOB,GPIO_PIN_15))



#define DCFAN_UPDATE_TIME       (1000)   //1s
#define DCFAN_MAXSPEED          (3000)   //1510 r/min
#define DCFAN_MINSPEED          (10)     //1/s   60r/min
#define DCFAN_MAXERROR          (10)     //10s
#define DCFAN_FG                (12)     //12 pulse per turn
#define CORR_FACTOR							(1)	 //correction factor
#define DCFAN_PERIOD            (1000)   //Duty = 1 10V
#define DCFAN_MAXDUTY           (500)    //Duty =     =6v   <900rpm
#define DCFAN_MINDUTY           (170)    //Duty =    >2.7v  100rpm
#define DCFAN_INITDUTY          (150)    //Duty = 400rpm


#define DCFAN_DEVIATION1        (200)
#define DCFAN_DEVIATION2        (100)
#define DCFAN_DEVIATION3        (50)
#define DCFAN_DEVIATION4        (25)
#define DCFAN_DEVIATION5        (5)
 
#define DCFAN_DUTY_ADJUST1      (0.01)    // >200  +10
#define DCFAN_DUTY_ADJUST2      (0.01)    // >100  +10
#define DCFAN_DUTY_ADJUST3      (0.006)   // >50   +6    
#define DCFAN_DUTY_ADJUST4      (0.004)   // >25   +4
#define DCFAN_DUTY_ADJUST5      (0.002)   // >5    +2


typedef struct 
{
    uint16_t AimSpeed;   
    uint16_t feedBackSpeed;
    uint16_t CtlTimeSpan;   //Update speed interval
    uint16_t IOstate;       //last feedback IO state
    uint16_t duty;
    uint16_t PulseCntLast;  //last feedback pulse count  
    uint16_t PulseCntCur;   //current pulse count
    uint16_t ErrorCnt;    
    uint16_t ErrorFlag; 
}dcfanParam;
extern dcfanParam g_DCfan1Param,g_DCfan2Param;
    
void DcFan_init(void);
void DcFan1_Ctrl(void);
void DcFan2_Ctrl(void);
void DcFan_FeedBack(void);
extern void Fan_FeedBack_Calc(void);
extern void Set_AimSpeed(uint8_t i,uint16_t value);
extern uint16_t Get_feedBackSpeed(uint8_t i);

extern uint8_t Hal_GetFanInput(uint8_t index);
#endif /*__FAN_CTRL_H */
